package sample.controller.todo;

import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.util.StringUtil;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;

public class EditController extends Controller {

    @SuppressWarnings("unused")
    private static final Logger logger =
        Logger.getLogger(EditController.class.getName());

    @Override
    public Navigation run() {

        String key = key();
        if (!StringUtil.isEmpty(key)) {

            DatastoreService datastoreService =
                DatastoreServiceFactory.getDatastoreService();

            try {
                Entity todo = datastoreService.get(KeyFactory.stringToKey(key));

                requestScope("key", KeyFactory.keyToString(todo.getKey()));
                requestScope("content", todo.getProperty("content"));

            } catch (EntityNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return forward("edit.jsp");
    }
}
